package com.zch.pilot.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zch.domain.Result;
import com.zch.entity.TbPilotProject;
import com.zch.pilot.service.TbPilotProjectService;
import com.zch.pilot.mapper.TbPilotProjectMapper;
import com.zch.pilot.vo.PilotVo;
import com.zch.pilot.vo.SearchVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 86134
* @description 针对表【tb_pilot_project(试验项目表
)】的数据库操作Service实现
* @createDate 2023-10-30 20:02:33
*/
@Service
public class TbPilotProjectServiceImpl extends ServiceImpl<TbPilotProjectMapper, TbPilotProject>
    implements TbPilotProjectService{

    @Autowired
    private TbPilotProjectMapper tbPilotProjectMapper;

    @Override
    public Result addPilot(PilotVo pilotVo) {
        TbPilotProject tbPilotProject = new TbPilotProject();
        BeanUtils.copyProperties(pilotVo,tbPilotProject);
        boolean save = save(tbPilotProject);
        return save?Result.SUCCESS():Result.FAILED(500,"添加失败");
    }

    @Override
    public Result updateAll(PilotVo pilotVo) {
        TbPilotProject tbPilotProject = new TbPilotProject();
        BeanUtils.copyProperties(pilotVo,tbPilotProject);
        boolean update = updateById(tbPilotProject);
        return update?Result.SUCCESS():Result.FAILED(500,"修改失败");
    }

    @Override
    public Result selectByPilot(PilotVo pilotVo) {
        QueryWrapper<TbPilotProject> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbPilotProject::getName,pilotVo.getName());
        TbPilotProject tbPilotProject = getOne(wrapper);
        return Result.SUCCESS(tbPilotProject);
    }

    @Override
    public Result deletedByID(PilotVo pilotVo) {
        boolean remove = removeById(pilotVo.getId());
        return remove?Result.SUCCESS():Result.FAILED(500,"删除失败");
    }

    List<PilotVo> getPilotData(){
        ArrayList<PilotVo> pilotVos = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            PilotVo pilotVo = new PilotVo();
            pilotVo.setId(i);
            pilotVo.setName("试验项目名称"+i);
            pilotVo.setUnitPrice(i);
            pilotVo.setStandard("标准"+i);
            pilotVos.add(pilotVo);
        }
        return pilotVos;
    }

    @Override
    public Result exportExcelFile(List<PilotVo> list, HttpServletResponse response) {
        String fileName = null;
        try {
            fileName = URLEncoder.encode("pilot", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), PilotVo.class)
                    .sheet("试验项目维护信息")
                    .doWrite(list);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return Result.SUCCESS();
    }

    @Override
    public Result importData(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, PilotVo.class, new AnalysisEventListener<PilotVo>() {
                @Override
                public void invoke(PilotVo pilotVo, AnalysisContext analysisContext) {
                    System.out.println("解析数据为"+pilotVo.toString());
                    addPilot(pilotVo);
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    System.out.println("解析完成");
                }
            }).sheet().doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return Result.SUCCESS();
    }

    @Override
    public Result listByPage(SearchVo searchVo) {
        //--1 查询实体page
        Page<TbPilotProject> page = new Page<>(searchVo.getPageNum(), searchVo.getPageSize());
        QueryWrapper<TbPilotProject> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(searchVo.getName())){
            wrapper.lambda().eq(TbPilotProject::getName,searchVo.getName());
        }
        Page<TbPilotProject> tbPilotProjectPage = tbPilotProjectMapper.selectPage(page, wrapper);

        //--2 实体page转VoPage
        Page<PilotVo> pilotVoPage = new Page<>();
        pilotVoPage.setTotal(tbPilotProjectPage.getTotal());
        List<PilotVo> pilotVos = tbPilotProjectPage.getRecords().stream().map(item -> {
            PilotVo pilotVo = new PilotVo();
            BeanUtils.copyProperties(item, pilotVo);
            return pilotVo;
        }).collect(Collectors.toList());
        pilotVoPage.setRecords(pilotVos);

        return Result.SUCCESS(pilotVoPage);
    }
}




